// pages/staff/search/search.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require("../../../utils/share.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        datestart: '起始时间',
        dateend: '结束时间',
        year: '',
        month: '',
        date: '',
        statueArr: ['全部', '潜在客户', '合作中'],
        statueindex: 0, //客户状态下标
        idenArr: ['全部', '未认证', '已认证'],
        idenindex: 0, //客户状态下标
        fromArr: [],
        fromindex: 0, //客户状态下标
        fromid: '',
        statu_id: 3,
        consultant_id: '',
        teamtype:'',
    },

    // 点击重置
    newclick: function(e) {
        this.setData({
            datestart: '起始时间',
            dateend: '结束时间',
            idenindex: 0, //客户状态下标
            fromindex: 0,
            statueindex: 0, //客户状态下标
            statu_id: 3,
            area: this.data.area, //区域数组
            area_index: 0, //区域下标，
            area_id: '', //地区id
            major_index: 0, //所报专业下标
            major: [{
                id: 0,
                title: '全部'
            }],
            major_id: '', //所报专业id
            course: [{
                id: 0,
                title: '全部'
            }], //所报课程
            course_index: 0, //所报课程下标
            course_id: '' //所报课程id

        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        this.setData({
            consultant_id: options.consultant_id,
            teamtype: options.teamtype,
        })
        if (wx.getStorageSync('keywordphone')) {
            wx.removeStorageSync('keywordphone');
        }
        this.fromis();
        // 调用区域函数
        var now = new Date();
        var year = now.getFullYear(); //得到年份
        var month = now.getMonth(); //得到月份
        var date = now.getDate(); //得到日期
        month = month + 1;
        date = date + 1;
        if (month < 10) month = "0" + month;
        if (date < 10) date = "0" + date;
        this.setData({
            year: year,
            month: month,
            date: date
        })
        // console.log(date);

    },

    fromis() {
        var that = this;
        share.wxRequest('crmapi/user/userPassive', {}, function(res) {
            // console.log(res)
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                // console.log(data);
                var obj = [{
                    id: 0,
                    name: '全部'
                }]
                that.setData({
                    fromArr: obj.concat(data)
                })
            }
            wx.hideLoading()
        })
    },


    // 点击客户状态
    statueclick: function(e) {
        // console.log(e);
        var index = e.target.dataset.index;
        this.setData({
            statueindex: index
        })
        if (Number(index) == 0) {
            this.setData({
                statu_id: 3
            })
        } else {
            this.setData({
                statu_id: Number(index)
            })
        }

    },

    formclick(e) {
        var index = e.target.dataset.index;
        var id = e.target.dataset.id;
        this.setData({
            fromindex: index,
            fromid: id,
        })
    },

    //  选择起始时间
    bindDateChangestart: function(e) {
        this.setData({
            datestart: e.detail.value
        })
    },
    // 结束时间
    bindDateChangeend: function(e) {
        // console.log(this.data.datestart == '起始时间');
        if (this.data.datestart == '起始时间') {
            wx.showToast({
                title: '请先选择起始时间',
                icon: 'none',
                duration: 1500
            })
        } else {
            this.setData({
                dateend: e.detail.value
            })
        }
    },



    // 点击提交
    submitclick: function() {
        var that = this;
        var submitdata = that.data;
        // 获取课程顾问id
        var consultant_id = that.data.consultant_id;
        // console.log(consultant_id);
        var fromindex = submitdata.fromindex;
        if (fromindex == 6 || fromindex == '6') {
            var fromindex1 = 7;
        } else {
            var fromindex1 = fromindex;
        }
        var fromtext = submitdata.fromArr[fromindex].name;
        if (this.data.datestart == '起始时间') {
            var parmdata = {
                consultant_id: consultant_id, //课程顾问ID
                status: this.data.statu_id, //客户状态（1潜在客户 2合作中）
                idenindex: this.data.idenindex, //是否认证 1:未认证 2:已认证
                fromindex: this.data.fromid,
                start_date: '',
                end_date: '',
            }
            var start_date = '';
            var end_date = '';
        } else {
            if (submitdata.dateend == '结束时间') {
                // console.log(this.data.year + "-" + this.data.month + "-" + this.data.date);
                var parmdata = {
                    consultant_id: consultant_id, //课程顾问ID
                    idenindex: submitdata.idenindex, //是否认证 1:未认证 2:已认证
                    fromindex: this.data.fromid,
                    status: submitdata.statu_id, //客户状态（1潜在客户 2合作中）
                    start_date: submitdata.datestart, //开始时间
                    end_date: submitdata.year + "-" + submitdata.month + "-" + submitdata.date, //结束时间 
                }
                var start_date = submitdata.datestart;
                var end_date = submitdata.year + "-" + submitdata.month + "-" + submitdata.date;

            } else {
                var parmdata = {
                    consultant_id: consultant_id, //课程顾问ID
                    status: submitdata.statu_id, //客户状态（1潜在客户 2合作中）
                    idenindex: submitdata.idenindex, //是否认证 1:未认证 2:已认证
                    fromindex: this.data.fromid,
                    start_date: submitdata.datestart, //开始时间
                    end_date: submitdata.dateend, //结束时间 
                }
                var start_date = submitdata.datestart;
                var end_date = submitdata.dateend;

            }
        }
        var prma = {
            status: submitdata.statueArr[submitdata.statueindex], //客户状态（1潜在客户 2合作中）
            idenindex: submitdata.idenArr[submitdata.idenindex], //是否认证 1:未认证 2:已认证
            fromindex: fromtext,
            start_date: start_date, //开始时间
            end_date: end_date, //结束时间 
        }
        // console.log(JSON.stringify(prma))
        // console.log(JSON.stringify(parmdata))
        wx.setStorageSync('prma', JSON.stringify(prma));
        wx.setStorageSync('parmdata', JSON.stringify(parmdata));
        if (Number(that.data.teamtype)!=1){
            wx.navigateTo({
                url: '../../staff/mycustom?teamtype=' + that.data.teamtype + '&consultant_id=' + that.data.consultant_id + '',
            })
        }else{
            wx.redirectTo({
                url: '../../staff/mycustom?teamtype=' + that.data.teamtype + '&consultant_id=' + that.data.consultant_id+'',
            })
        }
    },


})